package course.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.JTextComponent;

import course.controller.CourseControllerListener;
import course.po.Course;

public class CourseJPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private JTextField searchTextField;
	private static JScrollPane tablePanel;
	private static JPanel panel;
	public static int isbn = -1;
<<<<<<< HEAD
	public static int cI = 0;
	public static JComboBox<String> comboBox;
	private static CourseControllerListener controllerListener;

	public CourseJPanel() {
		this.controllerListener = new CourseControllerListener();
		this.controllerListener.setCoursePanel(this);
		setLayout(null);
		panel = new JPanel();
		panel.setBounds(15, 195, 1020, 387);
		panel.setBorder(new LineBorder(new Color(127, 157, 185), 1, false));
		panel.setLayout(new BorderLayout(0, 0));
		panel.add(this.getCourseTablePanel(controllerListener.getCourseService().findAllCourse()));
		add(panel);
		JPanel paneO = new JPanel();
		paneO.setBounds(15, 15, 1020, 150);
		paneO.setBorder(new LineBorder(new Color(127, 157, 185), 1, false));
		add(paneO);
		paneO.setLayout(null);

		JLabel searchLabel = new JLabel("搜索类型：");
		searchLabel.setFont(new Font("宋体", Font.PLAIN, 24));
		searchLabel.setBounds(97, 56, 126, 40);
		paneO.add(searchLabel);

		comboBox = new JComboBox<String>();// 创建一个下拉列表框comboBox
		comboBox.addItem("课程编号");
		comboBox.addItem("课程名");
		comboBox.setBounds(238, 62, 155, 33);
		paneO.add(comboBox);

		searchTextField = new JTextField();
		searchTextField.setBounds(461, 62, 245, 33);
		paneO.add(searchTextField);
		searchTextField.setColumns(10);

		JButton searchCourseButton = new JButton("查询");
		ImageIcon searchCourseIcon = new ImageIcon(CourseJPanel.class.getResource("/searchDean.png"));
		searchCourseButton.setIcon(searchCourseIcon);
		searchCourseButton.setBounds(779, 58, 106, 40);
		paneO.add(searchCourseButton);
		searchCourseButton.setFont(new Font("宋体", Font.PLAIN, 19));
		searchCourseButton.addActionListener(controllerListener);
		searchCourseButton.setActionCommand("searchCourseDialog");

		JPanel paneT = new JPanel();
		paneT.setBounds(15, 610, 1020, 86);
		add(paneT);
		paneT.setLayout(null);

		JButton updateCourseButton = new JButton("修改");
		ImageIcon updateCourseIcon = new ImageIcon(CourseJPanel.class.getResource("/updateDean.png"));
		updateCourseButton.setIcon(updateCourseIcon);
		updateCourseButton.setBounds(442, 15, 106, 40);
		paneT.add(updateCourseButton);
		updateCourseButton.setFont(new Font("宋体", Font.PLAIN, 19));
		updateCourseButton.addActionListener(controllerListener);
		updateCourseButton.setActionCommand("updateCourseDialog");

		JButton deleteCourseButton = new JButton("删除");
		ImageIcon deleteCourseIcon = new ImageIcon(CourseJPanel.class.getResource("/deleteDean.png"));
		deleteCourseButton.setIcon(deleteCourseIcon);
		deleteCourseButton.setBounds(716, 15, 106, 40);
		paneT.add(deleteCourseButton);
		deleteCourseButton.setFont(new Font("宋体", Font.PLAIN, 19));
		deleteCourseButton.addActionListener(controllerListener);
		deleteCourseButton.setActionCommand("deleteCourseDialog");

		JButton addCourseButton = new JButton("添加");
		ImageIcon addCourseIcon = new ImageIcon(CourseJPanel.class.getResource("/addDean.png"));
		addCourseButton.setIcon(addCourseIcon);
		addCourseButton.setBounds(156, 15, 106, 40);
		paneT.add(addCourseButton);
		addCourseButton.setFont(new Font("宋体", Font.PLAIN, 19));
		addCourseButton.addActionListener(controllerListener);
		addCourseButton.setActionCommand("addCourseDialog");
	}

	public static JScrollPane getCourseTablePanel(List<Course> courseList) {
		String titles[] = { "编号", "开课年份和学期", "课程号", "课程名", "时间/地点", "学分", "授课老师编号", "授课老师姓名", "课程容量", "已选人数" };
		Object[][] data = new Object[courseList.size()][10];
		Iterator<Course> iter = courseList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			Course course = iter.next();
			data[i][0] = course.getCouId();
			data[i][1] = course.getCouType();
			data[i][2] = course.getCouNo();
			data[i][3] = course.getCouName();
			data[i][4] = course.getCouDetail();
			data[i][5] = course.getCouCredit();
			data[i][6] = course.getTeacher().getTeaId();
			data[i][7] = course.getTeacher().getTeaName();
			data[i][8] = course.getMaxNumber();
			data[i][9] = course.getSelectNumber();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data, titles);
		JTable table = new JTable(tableModel);
		table.getTableHeader().setBackground(Color.orange); // 表头颜色
		table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 17)); // 表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 25)); // 表体字体
		table.setBackground(Color.pink); // 表体颜色
		table.getTableHeader().setPreferredSize(new Dimension(1, 30)); // 表头高度
		table.setRowHeight(table.getRowHeight() + 15); // 表体高度
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 表格只能选择一行
		table.repaint();
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				isbn = table.getSelectedRow(); // 得到选中的行
				int row = table.getSelectedRow();
				int column = table.getSelectedColumn();
//				StudentFrame.t2.setText(table.getValueAt(row, column).toString());
				cI = Integer.valueOf(table.getValueAt(row, 0).toString());
=======
	public static JComboBox<String> comboBox;
	private static CourseControllerListener controllerListener;

	public CourseJPanel() {
		this.controllerListener = new CourseControllerListener();
		this.controllerListener.setCoursePanel(this);
		setLayout(null);
		panel = new JPanel();
		panel.setBounds(15, 195, 1020, 387);
		panel.setBorder(new LineBorder(new Color(127, 157, 185), 1, false));
		panel.setLayout(new BorderLayout(0, 0));
		panel.add(this.getCourseTablePanel(controllerListener.getCourseService().findAllCourse()));
		add(panel);
		JPanel paneO = new JPanel();
		paneO.setBounds(15, 15, 1020, 150);
		paneO.setBorder(new LineBorder(new Color(127, 157, 185), 1, false));
		add(paneO);
		paneO.setLayout(null);

		JLabel searchLabel = new JLabel("搜索类型：");
		searchLabel.setFont(new Font("宋体", Font.PLAIN, 24));
		searchLabel.setBounds(97, 56, 126, 40);
		paneO.add(searchLabel);

		comboBox = new JComboBox<String>();// 创建一个下拉列表框comboBox
		comboBox.addItem("课程编号");
		comboBox.addItem("课程名");
		comboBox.setBounds(238, 62, 155, 33);
		paneO.add(comboBox);

		searchTextField = new JTextField();
		searchTextField.setBounds(461, 62, 245, 33);
		paneO.add(searchTextField);
		searchTextField.setColumns(10);

		JButton searchCourseButton = new JButton("查询");
		ImageIcon searchCourseIcon = new ImageIcon(CourseJPanel.class.getResource("/searchDean.png"));
		searchCourseButton.setIcon(searchCourseIcon);
		searchCourseButton.setBounds(779, 58, 106, 40);
		paneO.add(searchCourseButton);
		searchCourseButton.setFont(new Font("宋体", Font.PLAIN, 19));
		searchCourseButton.addActionListener(controllerListener);
		searchCourseButton.setActionCommand("searchCourseDialog");

		JPanel paneT = new JPanel();
		paneT.setBounds(15, 610, 1020, 86);
		add(paneT);
		paneT.setLayout(null);

		JButton updateCourseButton = new JButton("修改");
		ImageIcon updateCourseIcon = new ImageIcon(CourseJPanel.class.getResource("/updateDean.png"));
		updateCourseButton.setIcon(updateCourseIcon);
		updateCourseButton.setBounds(442, 15, 106, 40);
		paneT.add(updateCourseButton);
		updateCourseButton.setFont(new Font("宋体", Font.PLAIN, 19));
		updateCourseButton.addActionListener(controllerListener);
		updateCourseButton.setActionCommand("updateCourseDialog");

		JButton deleteCourseButton = new JButton("删除");
		ImageIcon deleteCourseIcon = new ImageIcon(CourseJPanel.class.getResource("/deleteDean.png"));
		deleteCourseButton.setIcon(deleteCourseIcon);
		deleteCourseButton.setBounds(716, 15, 106, 40);
		paneT.add(deleteCourseButton);
		deleteCourseButton.setFont(new Font("宋体", Font.PLAIN, 19));
		deleteCourseButton.addActionListener(controllerListener);
		deleteCourseButton.setActionCommand("deleteCourseDialog");

		JButton addCourseButton = new JButton("添加");
		ImageIcon addCourseIcon = new ImageIcon(CourseJPanel.class.getResource("/addDean.png"));
		addCourseButton.setIcon(addCourseIcon);
		addCourseButton.setBounds(156, 15, 106, 40);
		paneT.add(addCourseButton);
		addCourseButton.setFont(new Font("宋体", Font.PLAIN, 19));
		addCourseButton.addActionListener(controllerListener);
		addCourseButton.setActionCommand("addCourseDialog");
	}

	public static JScrollPane getCourseTablePanel(List<Course> courseList) {
		String titles[] = { "编号", "开课年份和学期", "课程号", "课程名", "时间/地点", "学分", "授课老师编号", "授课老师姓名", "课程容量", "已选人数" };
		Object[][] data = new Object[courseList.size()][10];
		Iterator<Course> iter = courseList.iterator();
		int i = 0;
		while (iter.hasNext()) {
			Course course = iter.next();
			data[i][0] = course.getCouId();
			data[i][1] = course.getCouType();
			data[i][2] = course.getCouNo();
			data[i][3] = course.getCouName();
			data[i][4] = course.getCouDetail();
			data[i][5] = course.getCouCredit();
			data[i][6] = course.getTeacher().getTeaId();
			data[i][7] = course.getTeacher().getTeaName();
			data[i][8] = course.getMaxNumber();
			data[i][9] = course.getSelectNumber();
			i++;
		}
		DefaultTableModel tableModel = new DefaultTableModel(data, titles);
		JTable table = new JTable(tableModel);
		table.getTableHeader().setBackground(Color.orange); // 表头颜色
		table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 17)); // 表头字体
		table.setFont(new Font("微软雅黑", Font.PLAIN, 25)); // 表体字体
		table.setBackground(Color.pink); // 表体颜色
		table.getTableHeader().setPreferredSize(new Dimension(1, 30)); // 表头高度
		table.setRowHeight(table.getRowHeight() + 15); // 表体高度
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 表格只能选择一行
		table.repaint();
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				isbn = table.getSelectedRow(); // 得到选中的行
				int row = table.getSelectedRow();
				int column = table.getSelectedColumn();
				StudentFrame.t2.setText(table.getValueAt(row, column).toString());
>>>>>>> refs/remotes/origin/master
			}
		});
		tablePanel = new JScrollPane(table);
		return tablePanel;
	}

	public static void refresh(List<Course> courseList) {
		System.out.println("测试——当前课程数量为：" + courseList.size());
		panel.remove(tablePanel);
		panel.add(CourseJPanel.getCourseTablePanel(controllerListener.getCourseService().findAllCourse()),
				BorderLayout.CENTER);
		panel.validate();
		JOptionPane.showMessageDialog(panel, "重新加载数据成功！");
	}

	public void showSearchResult(List<Course> result) {
		panel.remove(tablePanel);
		panel.add(this.getCourseTablePanel(result), BorderLayout.CENTER);
		this.validate();
		JOptionPane.showMessageDialog(this, "查询成功！");
	}

	public CourseControllerListener getControllerListener() {
		return controllerListener;
	}

	public int getIsbn() {
		return isbn;
	}

	public JTextComponent getSearchTextField() {
		return searchTextField;
	}
}
